<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { errorModal} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				instId:'',
				busCode:'',
				posListStr:'',
				existPosIdStr:'',
				hasUserStoreFlag:false,//人员储备标识
				hasDataFlag:false,//有子表数据标识
				isallNoOfficeFlag:false,//是否全部是“审批后不再任职”
				posIdsStr:'',
				adjustUserId:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			onInputProbation(num){
				if(this.formdata.newWorkDate){
					var d1 = this.moment(this.formdata.newWorkDate)
					var probationDate = d1.add(num, "days").format("YYYY-MM-DD")
					this.$refs.autoform.formdata.probationDate = probationDate
				}else if(this.formdata.probationDate){
					var d2 = this.moment(this.formdata.probationDate)
					var newWorkDate = d2.subtract(num, "days").format("YYYY-MM-DD")
					this.$refs.autoform.formdata.newWorkDate = newWorkDate
				}
			},
			onSelectWorkDate(item){
				var d1 = this.moment(item)
				if(this.formdata.probationDate){
					var d2 = this.moment(this.formdata.probationDate)
					var diff = d2.diff(d1, 'days')
					this.$refs.autoform.formdata.newProbation = diff
				}else if(this.formdata.newProbation){
					var probationDate = d1.add(this.formdata.newProbation, "days").format("YYYY-MM-DD")
					this.$refs.autoform.formdata.probationDate = probationDate
				}
			},
			//计算试用期到期日
			onSelectProbationDate(item){
				var d2 = this.moment(item)
				if(this.formdata.newWorkDate){
					var d1 = this.moment(this.formdata.newWorkDate)
					var diff = d2.diff(d1, 'days')
					this.$refs.autoform.formdata.newProbation = diff
				}else if(this.formdata.newProbation){
					var newWorkDate = d2.subtract(this.formdata.newProbation, "days").format("YYYY-MM-DD")
					this.$refs.autoform.formdata.newWorkDate = newWorkDate
				}
			},
			verifyReasonCode(item){
				if(item.code == "23753"){//选择人员储备
					if(this.hasDataFlag){
						return "人员储备只能选择一次，且不能包含其它事由"
					} 
					if(!this.isallNoOfficeFlag){
						return "“人员储备”必须是当前无任职岗位人员；所选人员当前有任职岗位，如做“人员储备”需将“原岗位处理方式”选择“审批后不再任职”方可办理！"
					}
				}
				return true
			},
			onSelectNewReason(item){
				let _that = this
				var ajaxArr = [{//验证人员增补事由，提示不能选择的岗位
					url:'hrPosUtil/verifyReasonCodeByH5',
					param: {
						reasonCode:item.code,
						busCode:this.busCode,
						posStr:this.posListStr,
						posIdRangeStr:this.existPosIdStr
					},
					method:'post'
				}]
				urlUtil.promiseAll(ajaxArr,function(res){
					if(res[0].succeed) {
						if(res[0].value.checkbox){
							errorModal(_that, res[0].value.field)
						}
						_that.$refs.autoform.changeEditor("newInfo","newPosId","data", configSelectData(res[0].value.attributes.pos,"posid","posname"))
						_that.$refs.autoform.initSelectIndex("newInfo", "newPosId")
					}
				})
				//人员调整
				if(item.code == "35506"){
					_that.$refs.autoform.changeEditor('newInfo', 'newReplaceUserId', {
						type:'selectbox',
						prompt:'请选择',
						required:true,
						data:[]
					});
				}else{
					_that.$refs.autoform.changeEditor('newInfo', 'newReplaceUserId', {
						type: 'labelbox',
						required:false,
						data:[]
					});
					_that.$refs.autoform.clear('newInfo', 'newReplaceUserId')
				}
			},
			clearNewReason(){
				let _that = this
				_that.$refs.autoform.clear("newInfo","newReasonCode")
				_that.$refs.autoform.changeEditor('newInfo', 'newReplaceUserId', {
					type: 'labelbox',
					required:false,
					data:[]
				});
				_that.$refs.autoform.clear('newInfo', 'newReplaceUserId')
			},
			onSelectNewPos(item){
				var appointPosId = ""
				var _that = this
				if(this.datas.length){
					appointPosIds = this.datas.filter((e,i)=> e.newWorkTypeCode == 20388 && ((hasValue(_that.index) && i != _that.index) || !hasValue(_that.index)))
					if(appointPosIds.length){
						appointPosId = appointPosIds[0]
					}
				}
				var ajaxArr = [{//任职方式
					url:'tempInstAdjust/getWorkTypeListByH5',
					param: {
						posId: item.posid,
						typeCode:this.formdata.newReasonCode,
						userId:this.adjustUserId,
						posIdsStr:this.posIdsStr,
						appointPosId:appointPosId
					}
				},{//上岗生效方式
					url:'tempInstAdjust/getNewPosWay1',
					param: {
						posId:item.posid,
						reasonCode:_that.formdata.newReasonCode
					}
				}]
				if(this.formdata.newReasonCode == "35506"){
					ajaxArr.push({
						url:'teRegister/getEmpByPosId',//根据岗位找现任职的人员
						param:{
							posId:item.posid
						}
					})
				}
				urlUtil.promiseAll(ajaxArr,function(res){
					_that.$refs.autoform.changeEditor('newInfo', 'newWorkTypeCode', 'data', configSelectData(res[0], 'code', 'dicname'));
					_that.$refs.autoform.initSelectIndex('newInfo', 'newWorkTypeCode')
					_that.$refs.autoform.changeEditor('newInfo', 'newEffectWayCode', 'data', configSelectData(res[1], 'code', 'dicname'));
					_that.$refs.autoform.initSelectIndex('newInfo', 'newEffectWayCode')
					if(res.length == 3){
						_that.$refs.autoform.changeEditor('newInfo', 'newReplaceUserId', 'data', configSelectData(res[2], 'userId', 'userName'));
						_that.$refs.autoform.initSelectIndex('newInfo', 'newReplaceUserId')
					}
				})
			},
			initLoad(){
				let _that = this
				//调岗事由
				_that.om.request('tempInstAdjust/getReasonCodeList', {
					instId: this.instId					
				}).then(res => {
					_that.$refs.autoform.changeEditor("newInfo","newReasonCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex("newInfo", "newReasonCode")
				});
				this.$nextTick(function(){
					this.$refs.autoform.changeEditor("newInfo", "newPosId", "data", configSelectData(JSON.parse(this.posListStr),"posid","posname"))
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.instId = e.instId
				_that.posListStr = e.posListStr
				_that.existPosIdStr = e.existPosIdStr
				_that.hasUserStoreFlag = e.hasUserStoreFlag
				_that.hasDataFlag = e.hasDataFlag
				_that.posIdsStr = e.posIdsStr
				_that.adjustUserId = e.adjustUserId
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.$nextTick(function(){
									this.initLoad()
								})
							}
						}
					});
				}
			})
		}
	}
</script>

<style>

</style>
